<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-1
 * Time: 下午6:31
 * To change this template use File | Settings | File Templates.
 */
class comment extends MY_Controller
{
    /**
     * 评论资源
     */
    public function save()
    {
        $id = intval($this->input->get_post('id'));
        $title = $this->input->get_post('title');
        $content = $this->input->get_post('content');

        $this->load->helper('validation');

        $response = array('error' => '0', 'msg' => '评论成功', 'code' => 'comment_success');

        do {
            if (empty ($title) || empty ($content) || empty ($id)) {
                $response = array('error' => '1001', 'msg' => '评论标题或内容为空', 'code' => 'comment_title_and_content_empty');
                break;
            }

            if (!length_limit($title, 5, 50)) {
                $response = array('error' => '1002', 'msg' => '评论标题小于5或大于50个字符', 'code' => 'title_of_the_review_is_less_than_5_or_more_than_50_characters');
                break;
            }

            if (!length_limit($content, 5, 150)) {
                $response = array('error' => '1003', 'msg' => '评论内容小于5或大于150个字符', 'code' => 'content_of_the_review_is_less_than_5_or_more_than_150_characters');
                break;
            }

            if (!$this->isLogin()) {
                $response = array('error' => '1004', 'msg' => '用户未登陆', 'code' => 'user_not_login');
                break;
            }

            $this->load->model('movie');
            $mData = $this->movie->getMovie($id);
            if (empty ($mData)) {
                $response = array('error' => '1005', 'msg' => '资源不存在', 'code' => 'resource_not_exist');
                break;
            }

            $data = array(
                'resource_id' => $id,
                'title' => $title,
                'content' => $content,
                'user_id' => $this->uInfo['id'],
                'username' => $this->uInfo['username'],
            );
            $this->load->model('comment');
            $id = $this->comment->save($data);
            if (!$id) {
                $response = array('error' => '1006', 'msg' => '评论失败', 'code' => 'comment_failure');
                break;
            }
        } while (false);

        self::json_output($response);
    }
}
